package com.wswy.wzcx.ui.main;

import android.os.SystemClock;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import timber.log.Timber;

public class MainActUtils {

    private static long lastReportedTimeMillis = 0;

    static final String DLG_KEY_POLICY = "policy";
    public static final String DLG_KEY_AD = "home_ad";
    static final String DLG_KEY_UPDATE = "update";
    private static boolean isShowing = false;
    private static Map<String, Runnable> dialogs = new HashMap<>();

    ///首页滑动
    static void report() {
        lastReportedTimeMillis = SystemClock.uptimeMillis();
    }

    public static long lastReportedTimeMillis() {
        return lastReportedTimeMillis;
    }


    public static long diffTime() {
        return SystemClock.uptimeMillis() - lastReportedTimeMillis;
    }


    //弹窗顺序
    public static void addNextDialog(String key, Runnable runnable) {
        dialogs.put(key, runnable);
        checkAndShow(false);
    }

    private static void popShowDialog() {
        Runnable runnable = getTopRunnable();
        Timber.e("----popShowDialog " + runnable);
        if (runnable != null) {
            isShowing = true;
            runnable.run();
            Timber.e("---run");
        }

    }

    public static void checkAndShow(boolean isEnd) {
        if (isEnd) {
            isShowing = false;
        }
        if (!dialogs.isEmpty() && !isShowing) {
            popShowDialog();
        }
    }

    static void dlgEnd() {
        dialogs.clear();
        isShowing = false;
    }

    private static Runnable getTopRunnable() {
        //弹窗 顺序
        String[] keys = {DLG_KEY_POLICY, DLG_KEY_UPDATE, DLG_KEY_AD};

        for (String key : keys) {
            Runnable reference = dialogs.remove(key);
            if (reference != null) {
                return reference;
            }
        }
        return null;
    }


}
